//
// server.hpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2012 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef HTTP_SERVER2_SERVER_HPP
#define HTTP_SERVER2_SERVER_HPP

#include <boost/asio.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include "connection.hpp"
#include "io_service_pool.hpp"
#include "lru_cache_using_boost.hpp"
/*
#include <cstddef>
#include <fstream>
#include <boost/make_shared.hpp>
#include <boost/log/core.hpp>
#include <boost/log/filters.hpp>
#include <boost/log/formatters.hpp>
#include <boost/log/attributes/constant.hpp>
#include <boost/log/attributes/scoped_attribute.hpp>
#include <boost/log/sources/channel_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/utility/init/common_attributes.hpp>
*/
namespace http {
    namespace server3 {

        /// The top-level class of the HTTP server.
        class server
            : private boost::noncopyable
        {
        public:
            /// Construct the server to listen on the specified TCP address and port, and
            /// serve up files from the given directory.
            explicit server(const std::string& address, const std::string& port, std::size_t io_service_pool_size);

            /// Run the server's io_service loop.
            void run();
            void initialize_log();
            static int request_rate;
            static long cache_hit;
            static long cache_miss;

            //added by KhanhHH
            //storing caching data
            typedef lru_cache_using_boost<std::string, std::string> lru_cache;
            static boost::shared_ptr<lru_cache> mobile_cache_ptr_;
            int static cache_size;

        private:
            /// Initiate an asynchronous accept operation.
            void start_accept();

            /// Handle completion of an asynchronous accept operation.
            void handle_accept(const boost::system::error_code& e);

            /// Handle a request to stop the server.
            void handle_stop();

            //Called by timer for calculate rate.
            void calculate_rate();

            /// The pool of io_service objects used to perform asynchronous operations.
            io_service_pool io_service_pool_;

            /// The signal_set is used to register for process termination notifications.
            boost::asio::signal_set signals_;

            /// Acceptor used to listen for incoming connections.
            boost::asio::ip::tcp::acceptor acceptor_;

            /// The next connection to be accepted.
            connection_ptr new_connection_;


            /// timer for ending connection after timeout.
            boost::asio::deadline_timer rate_timer_;
            int last_request_num;
            int refresh_rate_timeout;

        };

    } // namespace server2
} // namespace http

#endif // HTTP_SERVER2_SERVER_HPP
